package com.example.shop_2305.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseFragment
import com.example.shop_2305.R
import com.example.shop_2305.adapter.GoodsAdapter
import com.example.shop_2305.adapter.OneAdapter
import com.example.shop_2305.adapter.TwoAdapter
import com.example.shop_2305.databinding.FragmentCategoryBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

// TODO: Rename parameter arguments, choose names that match

class CategoryFragment : BaseFragment<FragmentCategoryBinding,CategoryViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsAdapter: GoodsAdapter
    var cid = 0//当前分类id
    var page = 1//当前页面
    var limit = 2
    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,4)


        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)
        //请求一级分类二级分类数据
        viewModel.category()
        //三级联动
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            //选中的发生变化
            oneAdapter.setId(oneAdapter.data[position].id)
            //数据发生变化
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
            //二级默认选中的也发生了变化
            cid = twoAdapter.data[0].id
            page = 1
            twoAdapter.setId(cid)

            viewModel.goods(cid,"",limit,false,page,"","")
        }
        //点击二级分类，选中发生变化  商品列表发生变化
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            cid = twoAdapter.data[position].id
            page = 1
            twoAdapter.setId(cid)
            viewModel.goods(cid,"",limit,false,page,"","")
        }
        binding.smart.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.goods(cid,"",limit,false,page,"","")
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(cid,"",limit,false,page,"","")
                binding.smart.finishLoadMore()
            }
        })

    }

    override fun initData() {
        viewModel.categorySuccess.observe(this){
            if(it.code==200){
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //二级分类数据
                twoAdapter.data.clear()
                twoAdapter.data.addAll(oneAdapter.data[0].child)
                twoAdapter.notifyDataSetChanged()
                //请求商品列表
                viewModel.goods(twoAdapter.data[0].id,"",20,false,1,"","")
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.categoryFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.goodsSuccess.observe(this){
            if (it.code==200){
                if(page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.goodsFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<CategoryViewModel> = CategoryViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_category

}